﻿
using CMS.Controls.Messages;
using CMS.Controls.TabPageTemplate;
using CMS.Infos;
using CMS.REST;
using Newtonsoft.Json.Linq;
using System;
using System.Windows.Forms;

namespace CMS.Controls.DisplayTabPages.SettingTabPages
{
    public class TermEditorAdapter : EditorAdaptor
    {
        public override Type ElementType => typeof(Term);

        public override string ItemName => "学期";

        public override string API => "apis/v1/terms";

        public override string PkField => "ID";

        public override string NameField => "Name";
        public override bool AllowIndexItem => Permission.CheckPermission(Permission.Term.Index);

        public override bool AllowCreateItem => Permission.CheckPermission(Permission.Term.Save);

        public override bool AllowUpdateItem => Permission.CheckPermission(Permission.Term.Update);

        public override bool AllowDeleteItem => false;

        private FormPanel FP_Main;
        private TextBox TB_Name;
        private DateTimePicker DTP_StartDate;
        private Button Bn_SetTermDefault;


        private int _DefaultTermID = 0;

        private void RefreshDefaultTermID()
        {
            RESTResponse response = RESTClient.HttpGet("apis/v1/cur_term_id");
            if (response.IsSuccess)
            {
                _DefaultTermID = response.Data[0].ToObject<int>();
            }
        }

        public override void InitializeEditor(Panel editorPanel, BindingSource source)
        {
            FP_Main = new FormPanel();
            _SettingFormPanel(editorPanel, FP_Main);
            FP_Main.SuspendLayout();
            TB_Name = new MyTextBox();
            TB_Name.DataBindings.Add("Text", source, "Name");
            FP_Main.AddField("学期可见名称", TB_Name, FormPanel.TextNoEmptyValidator);

            DTP_StartDate = new MyDateTimePicker();
            DTP_StartDate.DataBindings.Add("Value", source, "StartDateRaw");

            FP_Main.AddField("学期开始日期", DTP_StartDate, control =>
           {
               return DTP_StartDate.Value.DayOfWeek == DayOfWeek.Monday;
           });

            Bn_SetTermDefault = new MyButton() {Text=Permission.CheckPermission(Permission.SetDefaultTerm)?"设置默认学期":"没有权限" };
            Bn_SetTermDefault.Click += Bn_SetTermDefault_Click;

            FormBase.ColumnDefinition[] definitions = FP_Main.CloneColumnDefinition(false, true, false);
            definitions[1].Padding.Top = definitions[1].Padding.Bottom = 15;

            FP_Main.AddField("设置为默认学期", Bn_SetTermDefault,null, definitions);

            FP_Main.ResumeLayout();

            RefreshDefaultTermID();
        }

        private void Bn_SetTermDefault_Click(object sender, EventArgs e)
        {
            if (EditorSource.DataSource is Term term)
            {
                if (term.ID == 0)
                {
                    _SendMessage(MsgType.ERROR, "必须保存新学期才可以使用此功能");
                    return;
                }

                RESTResponse response = RESTClient.HttpPost("apis/v1/cur_term_id", new JObject()
                {
                    {"term_id",term.ID }
                });
                if (response.IsSuccess)
                {
                    _DefaultTermID = term.ID;
                    Bn_SetTermDefault.Enabled = false;
                    _SendMessage(MsgType.OK, "设置默认学期成功");
                }
                else
                {
                    _SendMessage(MsgType.WARN, "保存设置失败,可能是没有权限");
                }
            }
        }




        public override bool UpdateDisplayMode(Panel editorPanel, DisplayMode mode)
        {
            switch (mode)
            {
                case DisplayMode.New:
                    Bn_SetTermDefault.Enabled = false;
                    FP_Main.ReadOnly = false;
                    TB_Name.Enabled = DTP_StartDate.Enabled = true;
                    break;
                case DisplayMode.Edit:
                    Bn_SetTermDefault.Enabled = (EditorSource.DataSource as Term)?.ID != _DefaultTermID;
                    FP_Main.ReadOnly = false;
                    TB_Name.Enabled = DTP_StartDate.Enabled = true;
                    break;
                case DisplayMode.Preview:
                    FP_Main.ReadOnly = true;
                    TB_Name.Enabled = DTP_StartDate.Enabled = Bn_SetTermDefault.Enabled = false;
                    break;
                case DisplayMode.None:
                    FP_Main.ClearContent();
                    break;
                default:
                    return false;
            }
            return true;
        }

        public override bool ValidateForm()
        {
            return FP_Main.Validate();
        }
    }


}
